<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/1/13 0013
 * Time: 16:39
 * @property $common_model common_model
 */
class Partner extends MY_Controller
{

    private $t_user = 'user';  //用户表
    private $t_userinfo = 'user_login_log';  //用户日志表

    function __construct()
    {
        parent::__construct();
        //加载用户模型
        $this->load->model('users_model');
        $this->load->model('partner_model');
        $this->load->model('api_model/tom_account_model' , 'account_model');

        $this->lang->load('member');
    }

    public function inedx()
    {
        $this->MaintenanceProjects();
    }

    /**
     * 合作商维保项
     */
    public function MaintenanceProjects()
    {
        $customerList = $this->common_model->selectCustom('mt_customers' , array('name !=' => '') , null , array('type' => array('1' , '2')));
        $data['customerList'] = $customerList;

        //查询品牌列表
        $brandList = $this->db->query("SELECT * FROM `site_product_brand` GROUP BY	bid,tid")->result_array();
        foreach ($brandList as $k => $v) {
            $brand = $this->db->query(sprintf("select id,sap_id from product_brand where sap_id = '%s'" , $v['bid']))->row_array();
            $brandList[$k]['mbrandId'] = $brand['id'];
            $brandList[$k]['sap_id'] = $brand['sap_id'];
        }
        $data['brandList'] = $brandList;


        //查询产品型号 begin
        $modelList = $this->db->query("select p.id,p.`name`,pab.tid,pab.bid,pab.mid from product_model p inner join product_association_b  pab on p.sap_id = pab.mid")->result_array();
        $data['modelList'] = $modelList;

        //查询产品型号 end
        //查询产品类型
        $categoryList = $this->common_model->selectCustom('mt_product_category');
        $data['categoryList'] = $categoryList;


        //维修点列表
        $siteList = $this->common_model->selectCustom('mt_service_center');
        $data['siteList'] = $siteList;

        //维修点列表
        $gradeList = $this->common_model->selectCustom('mt_repair_grade');
        $data['gradeList'] = $gradeList;

        //随机附件
        $accessory = $this->common_model->selectCustom('mt_product_accessory');
        foreach ($accessory as $k => $v) {
            $categoryFirst = $this->common_model->selectFirst('mt_product_category' , array('id' => $v['category']));
            $accessory[$k]['typeName'] = $categoryFirst['name_' . $this->curLang];
        }
        $data['accessory'] = $accessory;

        //故障项
        $fault = $this->common_model->selectCustom('mt_product_fault');
        foreach ($fault as $k => $v) {
            $categoryFirst = $this->common_model->selectFirst('mt_product_category' , array('id' => $v['category']));
            $fault[$k]['typeName'] = $categoryFirst['name_' . $this->curLang];
        }
        $data['fault'] = $fault;

        //维修项
        $ri = $this->common_model->selectCustom('mt_product_repair_item');

        //重构数据
        $availableArr = array('delete' , 'normal' , 'disable');
        foreach ($ri as $key => &$rs) {
            $rs['availableName'] = $availableArr[$rs['available']];
            $rs['isDefault'] = $rs['isDefault'] ? 'Yes' : 'No';
            $gradeFirst = $this->common_model->selectFirst('mt_repair_grade' , array('id' => $rs['grade']));
            $categoryFirst = $this->common_model->selectFirst('mt_product_category' , array('id' => $rs['category']));
            $rs['gradeName'] = $gradeFirst['name_' . $this->curLang];
            $rs['categoryName'] = $categoryFirst['name_' . $this->curLang];
            $rs['add_time'] = date('Y-m-d H:i:s' , $rs['add_time']);
        }

        $data['ri'] = $ri;
        $this->load->view('defaults/partner/mp' , $data);
        $this->load->view('defaults/common/footer');
    }

    public function ajaxLoadBrand()
    {
        $gp = gp();
        $sap_id = intval($gp['sapid']); //获取sapid

        $brandIds = $this->common_model->selectCustom('product_association_a' , array('tid' => $sap_id));
        $str_bids = explode(',' , $brandIds['bid']);
        print_r($brandIds);
    }

    public function ajaxLoadModel()
    {
        $gp = gp();
        $tid = intval($gp['tid']);
        $bid = intval($gp['bid']);
        $list = $this->common_model->selectCustom('product_association_b' , array('tid' => $tid , 'bid' => $bid));
        $str = '';
        foreach ($list as $k => $v) {
            $str .= $v['mid'] . ',';
        }
        $str = substr($str , 0 , -1);
        $result = $this->db->query(sprintf("SELECT * from `product_model` where `sap_id` in(%s)" , $str))->result_array();
        echo json_encode($result);
        exit;
    }

    /**
     * 复制一条随机配件数据
     */
    public function ajaxAddAccessory()
    {
        $gp = gp();
        $baseId = $gp['baseId']; //产品随机附件表id
        $catId = $gp['catId']; //产品分类id
        $brandId = $gp['brandId']; //品牌id
        $merchant_id = $gp['merchant_id']; //商家Id
        $model_id = $gp['model_id']; //商家Id
        $res = $this->partner_model->AddCustomizeAccessoryByBaseId($baseId , $catId , $brandId , $merchant_id , $model_id);
        echo($res);
        exit;
    }

    /**
     * 复制一条随机配件数据
     */
    public function ajaxLoadAccessory()
    {
        $gp = gp();
        $merchant_id = $gp['merchant_id']; //商家Id
        $res = $this->partner_model->LoadCustomizeAccessoryByMerchantId($merchant_id);
        echo(json_encode($res));
        exit;
    }

    /**
     * 修改值
     */
    public function ajaxMdyCustomizeAccessory()
    {
        $gp = gp();
        $id = $gp['id']; //
        $filed = $gp['filed']; //修改的字段
        $value = $gp['val']; //修改的值
        $res = $this->partner_model->MdyCustomizeAccessory($id , $filed , $value);
        echo(json_encode($res));
        exit;
    }

    /**
     * 删除
     */
    public function ajaxDelCustomizeAccessory()
    {
        $gp = gp();
        $id = $gp['id']; //
        $res = $this->partner_model->DelCustomizeAccessory($id);
        echo(json_encode($res));
        exit;
    }

    // 以下为 自定义故障代码****************************************************************************************************

    /**
     * 复制一条随机配件数据
     */
    public function ajaxAddFault()
    {
        $gp = gp();
        $baseId = $gp['baseId']; //产品随机附件表id
        $catId = $gp['catId']; //产品分类id
        $brandId = $gp['brandId']; //品牌id
        $merchant_id = $gp['merchant_id']; //商家Id
        $model_id = $gp['model_id']; //商家Id
        $res = $this->partner_model->AddCustomizeFaultByBaseId($baseId , $catId , $brandId , $merchant_id , $model_id);
        echo($res);
        exit;
    }

    /**
     * 复制一条随机配件数据
     */
    public function ajaxLoadFault()
    {
        $gp = gp();
        $merchant_id = $gp['merchant_id']; //商家Id
        $res = $this->partner_model->LoadCustomizeFaultByMerchantId($merchant_id);
        echo(json_encode($res));
        exit;
    }

    /**
     * 修改值
     */
    public function ajaxMdyCustomizeFault()
    {
        $gp = gp();
        $id = $gp['id']; //
        $filed = $gp['filed']; //修改的字段
        $value = $gp['val']; //修改的值
        $res = $this->partner_model->MdyCustomizeFault($id , $filed , $value);
        echo(json_encode($res));
        exit;
    }

    /**
     * 删除
     */
    public function ajaxDelCustomizeFault()
    {
        $gp = gp();
        $id = $gp['id']; //
        $res = $this->partner_model->DelCustomizeFault($id);
        echo(json_encode($res));
        exit;
    }

    /*     * **************************************************************************************************************************** */

    /**
     * 复制一条随机配件数据
     */
    public function ajaxAddItem()
    {
        $gp = gp();
        $baseId = $gp['baseId']; //产品随机附件表id
        $catId = $gp['catId']; //产品分类id
        $brandId = $gp['brandId']; //品牌id
        $merchant_id = $gp['merchant_id']; //商家Id
        $model_id = $gp['model_id']; //产品型号

        $res = $this->partner_model->AddCustomizeItemByBaseId($baseId , $catId , $brandId , $merchant_id , $model_id);
        echo($res);
        exit;
    }

    public function ajaxLoadItem()
    {
        $gp = gp();
        $merchant_id = $gp['merchant_id']; //商家Id
        $res = $this->partner_model->LoadCustomizeItemByMerchantId($merchant_id);
        echo(json_encode($res));
        exit;
    }

    /**
     * 删除
     */
    public function ajaxDelCustomizeItem()
    {
        $gp = gp();
        $baseid = $gp['laborid'];
        $catid = $gp['catid'];
        $brandid = $gp['brandid'];
        $cusid = $gp['cusid'];
        $res = $this->partner_model->DelCustomizeItem($baseid , $catid , $brandid , $cusid);
        echo(json_encode($res));
        exit;
    }

    /**
     * ajax调用各个站点的基数报价
     */
    public function ajaxGetCustomerPrice()
    {
        $gp = gp();
        $labor_id = $gp['lid']; //
        $mer_id = $gp['mid']; //
        $res = $this->partner_model->GetCustomerPrice($labor_id , $mer_id);
        echo(json_encode($res));
        exit;;
    }

    /**
     * @return string
     * ajax修改维修项
     * sqingwen
     * 2017/02/10
     */
    public function ajaxMdyItem()
    {
        $gp = gp();
        $cnname = trim($gp['cnname']);
        $enname = trim($gp['enname']);
        $category = trim($gp['category']);
        $grade = trim($gp['grade']);
        $price = trim($gp['price']);
        $sites = trim($gp['siteIds']);
        $cusId = trim($gp['cusId']);
        $laborId = trim($gp['laborId']);
        //重组价格的数组
        $price_arr = explode(',' , $price);
        $site_arr = explode(',' , $sites);
        $brand_id = trim($gp['brandId']);
        $model_id = trim($gp['modelId']);
        $res = 0;
        for ($ii = 0 ; $ii < count($price_arr) ; $ii++) {
            //这里面是按照站点id序号的方式来处理的
            //如果存在则修改
            if ($this->partner_model->CheckCustomizeItemIsExit($cusId , $laborId , $site_arr[$ii] , $brand_id , $category , $model_id) > 0) {
                $res = $this->partner_model->MdyCustomizeItemByFilter($category , $grade , $price_arr[$ii] , $brand_id , $cnname , $enname , $cusId , $laborId , $site_arr[$ii] , $model_id);
            }
            else {//如果不存在则插入
                $res = $this->partner_model->AddCustomizeItem($site_arr[$ii] , $category , $cusId , $grade , $laborId , $price_arr[$ii] , $price_arr[$ii] , '0' , $brand_id , $cnname , $enname , $model_id);
            }
        }
        echo($res);
        exit;
    }

    /**
     * ajax添加检测费用
     */
    public function ajaxAddTestingPrice()
    {
        $gp = gp();
        $name_cn = $gp['name_cn']; //品牌名称'
        $name_en = $gp['name_en']; //'品牌名称'
        $category_id = $gp['category_id']; //产品类型id'
        $brand_id = $gp['brand_id']; //品牌id'
        $merchant_id = $gp['merchant_id']; //商家id'
        $model_id = $gp['model_id']; //型号id
        $str_price = $gp['price_arr'];
        $arr = explode("&" , $str_price);
        $price_arr = array();
        foreach ($arr as $k => $v) {
            $tmp = explode("=" , $v);
            $tmp_key = str_replace('price_' , '' , $tmp[0]);
            $price_arr[$tmp_key] = $tmp[1];
        }

        $this->partner_model->AddInsFee($name_cn , $name_en , $category_id , $brand_id , $merchant_id , $model_id , $price_arr);
        $res = $this->partner_model->GetInsPrice($merchant_id);
        echo json_encode($res);
        exit;
    }

    /**
     * ajax获取报价
     */
    public function ajaxGetPrice()
    {
        $gp = gp();
        $merchant_id = $gp['merchant_id'];
        $res = $this->partner_model->GetInsPrice($merchant_id);
        echo json_encode($res);
        exit;
    }

    /**
     * 删除
     */
    public function ajaxDelPrice()
    {
        $gp = gp();
        $id = $gp['id'];
        $res = $this->partner_model->DelInsPrice($id);
        echo json_encode($res);
        exit;
    }

}
